package com.passive.lock.service.imp;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.passive.lock.mapper.TDictMapper;
import com.passive.lock.model.po.TDict;
import com.passive.lock.model.po.TDictExample;
import com.passive.lock.model.vo.TDictVo;
import com.passive.lock.model.vo.TUserVo;
import com.passive.lock.service.DictService;
import com.passive.lock.utils.DozerMapperUtils;
import com.passive.lock.utils.Result;


@Service("dictService")
public class DictServiceImpl implements DictService {

	@Resource(name = "TDictMapper")
	private TDictMapper dictMapper;

	@Override
	public Result deleteDict(TDictVo tDictVo) {	
		TDictExample example = new TDictExample();
		TDictExample.Criteria criteria = example.createCriteria();
		criteria.andPidEqualTo(tDictVo.getId());
		dictMapper.deleteByExample(example);
		dictMapper.deleteByPrimaryKey(tDictVo.getId());
		return Result.ok();
	}

	@Override
	public Result updateDict(TDictVo tDictVo) {
		TDict dict = new TDict();
		DozerMapperUtils.map(tDictVo, dict);
		dictMapper.updateByPrimaryKey(dict);
		List<TDict> tDicts = tDictVo.getDictVos();
		TDictExample example = new TDictExample();
		TDictExample.Criteria criteria = example.createCriteria();
		criteria.andPidEqualTo(dict.getId());
		dictMapper.deleteByExample(example);
		for (TDict vo : tDicts) {
			TDict children = new TDict();
			DozerMapperUtils.map(vo, children);
			children.setPid(dict.getId());
			dictMapper.insert(children);	
		}
		return Result.ok();
	}

	@Override
	public Result addDict(TDictVo tDictVo) {
		TDict dict = new TDict();
		DozerMapperUtils.map(tDictVo, dict);
		dict.setNum(0);
		dict.setPid(0);
		dictMapper.insert(dict);
		List<TDict> tDicts = tDictVo.getDictVos();
		for (TDict vo : tDicts) {
			TDict children = new TDict();
			DozerMapperUtils.map(vo, children);
			children.setPid(dict.getId());
			dictMapper.insert(children);	
		}

		return Result.ok();
	}

	@Override
	public Result dictList() {
		TDictExample example = new TDictExample();
		TDictExample.Criteria criteria = example.createCriteria();
		criteria.andPidEqualTo(0);
		List<TDict> list = dictMapper.selectByExample(example);
		List<TDictVo> listvo = new ArrayList<>();
		for (TDict tDict : list) {
			TDictVo tDdictVo = new TDictVo();
			StringBuilder detail = new StringBuilder();
			TDictExample ex = new TDictExample();
			TDictExample.Criteria cr = ex.createCriteria();
			cr.andPidEqualTo(tDict.getId());
			List<TDict> dicts = dictMapper.selectByExample(ex);
			if (dicts != null) {
				for (TDict dict : dicts) {
					detail.append(dict.getNum() + ":" + dict.getName() + ",");
				}
				DozerMapperUtils.map(tDict, tDdictVo);
				tDdictVo.setDetail(detail);
				tDdictVo.setDictVos(dicts);
				listvo.add(tDdictVo);
			}
		}
		return Result.ok(listvo);
	}


	@Override
	public Result dictChildren(Integer id) {
		TDictExample example = new TDictExample();
		TDictExample.Criteria criteria = example.createCriteria();
		criteria.andPidEqualTo(id);
		List<TDict> list = dictMapper.selectByExample(example);
		return Result.ok(list);
	}

}
